package SA.student.student;

import SA.student.MainActivity;
import SA.student.R;
import SA.student.R.drawable;
import SA.student.R.id;
import SA.student.R.layout;
import SA.student.notice.noticeActivity1;
import SA.student.question.questionActivity1;
import SA.student.shuttle.shuttleActivity1;
import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

import java.util.*;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.View.OnClickListener;

public class studentActivity1 extends Activity {

	ArrayList<TextView> list;
	TextView calendarTitle;
	int firstDay;
	int totDays;
	int iYear;
	int iMonth;
	int[] barImageArray = new int[] { R.drawable.zerobar, R.drawable.tenbar,
			R.drawable.twentybar, R.drawable.thirtybar, R.drawable.fourtybar,
			R.drawable.fiftybar, R.drawable.sixtybar, R.drawable.seventybar,
			R.drawable.eightybar, R.drawable.ninetybar, R.drawable.onehundbar };
	ImageView attendImage;
	ImageView homeworkImage;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.student1);
		Log.e("startActivity", "1");

		Calendar calendar = Calendar.getInstance();
		iYear = calendar.get(Calendar.YEAR);
		iMonth = calendar.get(Calendar.MONTH);
		calendarTitle = (TextView) findViewById(R.id.today);
		list = new ArrayList<TextView>();

		attendImage = (ImageView) findViewById(R.id.attendbar);
		homeworkImage = (ImageView) findViewById(R.id.homeworkbar);

		TableLayout table = (TableLayout) findViewById(R.id.calendar);

		int i, j;
		for (i = 0; i < 6; i++) {
			TableRow tr = new TableRow(this);
			for (j = 0; j < 7; j++) {
				TextView tv = new TextView(this);
				if (j == 0)
					tv.setTextColor(Color.RED);
				else if (j == 6)
					tv.setTextColor(Color.BLUE);
				else
					tv.setTextColor(Color.BLACK);
				tv.setTextSize(25);
				tv.setGravity(Gravity.CENTER_HORIZONTAL);
				tr.addView(tv);
				list.add(tv);
			}
			table.addView(tr);
		}
		table.setStretchAllColumns(true);
		setCalendar(iYear, iMonth);

		ImageView preBtn = (ImageView) findViewById(R.id.pre);
		preBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				iMonth--;
				if (iMonth <= 0) {
					iYear--;
					iMonth = 11;
				}
				setCalendar(iYear, iMonth);
			}
		});

		ImageView nextBtn = (ImageView) findViewById(R.id.next);
		nextBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				iMonth++;
				if (iMonth >= 12) {
					iMonth = 0;
					iYear++;
				}
				setCalendar(iYear, iMonth);
			}
		});

		TextView noticeBtn = (TextView) findViewById(R.id.notice);

		noticeBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				noticeActivity();
			}

		});

		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);

		mainBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				mainActivity();
			}

		});

		ImageView questionBtn = (ImageView) findViewById(R.id.questionBtn);

		questionBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				questionActivity();
			}

		});

		ImageView shuttleBtn = (ImageView) findViewById(R.id.shuttleBtn);

		shuttleBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				shuttleActivity();
			}

		});

	}

	void noticeActivity() {
		Intent intent = new Intent(this, noticeActivity1.class);
		startActivity(intent);
	}

	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}

	void questionActivity() {
		Intent intent = new Intent(this, questionActivity1.class);
		startActivity(intent);
	}

	void shuttleActivity() {
		Intent intent = new Intent(this, shuttleActivity1.class);
		startActivity(intent);
	}

	private void setCalendar(final int year, final int month) {
		Calendar todayCalendar = Calendar.getInstance();
		int todayYear;
		int todayMonth;
		int todayDate;

		todayYear = todayCalendar.get(Calendar.YEAR);
		todayMonth = todayCalendar.get(Calendar.MONTH);
		todayDate = todayCalendar.get(Calendar.DATE);

		int i;
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		calendarTitle.setText(year + "년" + (month + 1) + "월");

		int whatDay = calendar.get(Calendar.DAY_OF_WEEK);
		int j = 1;
		for (i = 0; i < list.size(); i++) {
			list.get(i).setText("");
		}

		// htmltext = j++ + "" + "<img src='attend.png'/>";
		// list.get(i).setText(
		// Html.fromHtml(htmltext, new ImageGetter(), null));

		// attendCount++;

		String htmltext;
		boolean attend = true;
		int attendCount = 0;
		int totalAttendCount = 0;
		int attendanceRate;

		for (i = whatDay - 1; i < calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + whatDay - 1; i++) {
			if ((year == todayYear) && (month == todayMonth)&& (j == todayDate)) {
				list.get(i).setBackgroundColor(Color.rgb(185, 225, 255));
			}else{
				list.get(i).setBackgroundColor(Color.WHITE);
			}
			if(attend){
				htmltext = j++ + "" + "<img src='attend.png'/>";
				list.get(i).setText(Html.fromHtml(htmltext, new ImageGetter(), null));
			}
			else
				list.get(i).setText(j++ + "");

			final int today = j;
			list.get(i).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					AlertDialog.Builder bld = new AlertDialog.Builder(studentActivity1.this);
					bld.setTitle(year + "년" + (month + 1) + "월" + (today - 1)+ "일");
					bld.setMessage("여기다 수업일정적어주셈");
					bld.show();
				}
			});
		}

		// attendCount = 20;
		// attendImage.setImageResource(barImageArray[10]);

		// totalAttendCount=calendar.getActualMaximum( Calendar.DAY_OF_MONTH );
		// attendanceRate = (float)((attendCount/totalAttendCount)*100);
		attendanceRate = 40;
		if (attendanceRate == 100) {
			attendImage.setImageResource(barImageArray[10]);
		} else if (attendanceRate >= 90) {
			attendImage.setImageResource(barImageArray[9]);
		} else if (attendanceRate >= 80) {
			attendImage.setImageResource(barImageArray[8]);
		} else if (attendanceRate >= 70) {
			attendImage.setImageResource(barImageArray[7]);
		} else if (attendanceRate >= 60) {
			attendImage.setImageResource(barImageArray[6]);
		} else if (attendanceRate >= 50) {
			attendImage.setImageResource(barImageArray[5]);
		} else if (attendanceRate >= 40) {
			attendImage.setImageResource(barImageArray[4]);
		} else if (attendanceRate >= 30) {
			attendImage.setImageResource(barImageArray[3]);
		} else if (attendanceRate >= 20) {
			attendImage.setImageResource(barImageArray[2]);
		} else if (attendanceRate > 0 && attendanceRate < 20) {
			attendImage.setImageResource(barImageArray[1]);
		} else if (attendanceRate == 0) {
			attendImage.setImageResource(barImageArray[0]);
		}
		// /////////////////////////////////////////////////////
		float homworkRate = 20;
		if (homworkRate == 100) {
			homeworkImage.setImageResource(barImageArray[10]);
		} else if (homworkRate >= 90) {
			homeworkImage.setImageResource(barImageArray[9]);
		} else if (homworkRate >= 80) {
			homeworkImage.setImageResource(barImageArray[8]);
		} else if (homworkRate >= 70) {
			homeworkImage.setImageResource(barImageArray[7]);
		} else if (homworkRate >= 60) {
			homeworkImage.setImageResource(barImageArray[6]);
		} else if (homworkRate >= 50) {
			homeworkImage.setImageResource(barImageArray[5]);
		} else if (homworkRate >= 40) {
			homeworkImage.setImageResource(barImageArray[4]);
		} else if (homworkRate >= 30) {
			homeworkImage.setImageResource(barImageArray[3]);
		} else if (homworkRate >= 20) {
			homeworkImage.setImageResource(barImageArray[2]);
		} else if (homworkRate > 0 && attendanceRate < 20) {
			homeworkImage.setImageResource(barImageArray[1]);
		} else if (homworkRate == 0) {
			homeworkImage.setImageResource(barImageArray[0]);
		}

	}

	private class ImageGetter implements Html.ImageGetter {
		public Drawable getDrawable(String source) {
			source = source.replaceAll(".png", "");
			int id = getResources().getIdentifier(source, "drawable",
					getPackageName());
			Drawable d = getResources().getDrawable(id);
			int w = d.getIntrinsicWidth();
			int h = d.getIntrinsicHeight();
			d.setBounds(0, 0, 9, 9);
			return d;
		}
	}

}
